package org.javaforever.cookieshop.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.javaforever.cookieshop.domain.PermissionBean;
import org.javaforever.cookieshop.service.PermissionService;
import org.javaforever.cookieshop.utils.BooleanUtil;
import org.javaforever.cookieshop.utils.PDFUtil;
import org.javaforever.cookieshop.utils.POIExcelUtil;
import org.javaforever.cookieshop.utils.POIPPTUtil;
import org.javaforever.cookieshop.utils.StringUtil;
import org.javaforever.cookieshop.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "/permissionController")
@RestController
public class PermissionController {
	public PermissionBean tempPermission = new PermissionBean();
	@Autowired
	protected PermissionService service;

	@RequestMapping(value = "/activateAllPermissions", method = RequestMethod.POST)
	public Map<String,Object> activateAllPermissions(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateAllPermissions(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/activatePermission", method = RequestMethod.POST)
	public Map<String,Object> activatePermission(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activatePermission(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/addPermission", method = RequestMethod.POST)
	public Map<String,Object> addPermission(@RequestBody PermissionBean permission) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.addPermission(permission);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/checkAccessPermission", method = RequestMethod.GET)
	public Map<String,Object> checkAccessPermission() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneAllPermissions", method = RequestMethod.POST)
	public Map<String,Object> cloneAllPermissions(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneAllPermissions(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/clonePermission", method = RequestMethod.POST)
	public Map<String,Object> clonePermission(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.clonePermission(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteAllPermissions", method = RequestMethod.POST)
	public Map<String,Object> deleteAllPermissions(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteAllPermissions(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deletePermission", method = RequestMethod.POST)
	public Map<String,Object> deletePermission(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deletePermission(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/exportPPTPermissions")
	public void exportPPTPermissions(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Permissions.pptx");
            List<PermissionBean> list = service.listAllPermissions();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "许可信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (PermissionBean permission:list) {
                Object [] row = {StringUtil.toNullString(permission.getId()),permission.getPermName(),StringUtil.toNullString(permission.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportPermissions")
	public void exportPermissions(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Permissions.xls");
            List<PermissionBean> list = service.listAllPermissions();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "许可信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (PermissionBean permission:list) {
                Object [] row = {StringUtil.toNullString(permission.getId()),permission.getPermName(),StringUtil.toNullString(permission.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportPermissionsPDF")
	public void exportPermissionsPDF(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Permissions.pdf");
            List<PermissionBean> list = service.listAllPermissions();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "许可信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (PermissionBean permission:list) {
                Object [] row = {StringUtil.toNullString(permission.getId()),permission.getPermName(),StringUtil.toNullString(permission.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportPermissionsWord")
	public void exportPermissionsWord(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Permissions.doc");
            List<PermissionBean> list = service.listAllPermissions();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "许可信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (PermissionBean permission:list) {
                Object [] row = {StringUtil.toNullString(permission.getId()),permission.getPermName(),StringUtil.toNullString(permission.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterPPTPermissions", method = RequestMethod.GET)
	public void filterPPTPermissions(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value = "permName", required = false) String permName,@RequestParam(value = "active", required = false) String active) throws Exception{
        PermissionBean permission0 = new PermissionBean();
        if (!StringUtil.isBlank(permName)) permission0.setPermName(permName);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) permission0.setActive(activeBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Permissions.pptx");
            List<PermissionBean> list = service.searchPermissionsByFields(permission0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "许可信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (PermissionBean permission:list) {
                Object [] row = {StringUtil.toNullString(permission.getId()),permission.getPermName(),StringUtil.toNullString(permission.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterPermissionsExcel", method = RequestMethod.GET)
	public void filterPermissionsExcel(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value = "permName", required = false) String permName,@RequestParam(value = "active", required = false) String active) throws Exception{
        PermissionBean permission0 = new PermissionBean();
        if (!StringUtil.isBlank(permName)) permission0.setPermName(permName);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) permission0.setActive(activeBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Permissions.xls");
            List<PermissionBean> list = service.searchPermissionsByFields(permission0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "许可信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (PermissionBean permission:list) {
                Object [] row = {StringUtil.toNullString(permission.getId()),permission.getPermName(),StringUtil.toNullString(permission.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterPermissionsPDF", method = RequestMethod.GET)
	public void filterPermissionsPDF(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value = "permName", required = false) String permName,@RequestParam(value = "active", required = false) String active) throws Exception{
        PermissionBean permission0 = new PermissionBean();
        if (!StringUtil.isBlank(permName)) permission0.setPermName(permName);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) permission0.setActive(activeBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Permissions.pdf");
            List<PermissionBean> list = service.searchPermissionsByFields(permission0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "许可信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (PermissionBean permission:list) {
                Object [] row = {StringUtil.toNullString(permission.getId()),permission.getPermName(),StringUtil.toNullString(permission.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterPermissionsWord", method = RequestMethod.GET)
	public void filterPermissionsWord(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value = "permName", required = false) String permName,@RequestParam(value = "active", required = false) String active) throws Exception{
        PermissionBean permission0 = new PermissionBean();
        if (!StringUtil.isBlank(permName)) permission0.setPermName(permName);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) permission0.setActive(activeBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Permissions.doc");
            List<PermissionBean> list = service.searchPermissionsByFields(permission0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "许可信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (PermissionBean permission:list) {
                Object [] row = {StringUtil.toNullString(permission.getId()),permission.getPermName(),StringUtil.toNullString(permission.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/findPermissionById", method = RequestMethod.POST)
	public Map<String,Object> findPermissionById(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        PermissionBean permission = service.findPermissionById(id);
        result.put("success",true);
        result.put("data",permission);
        return result;
	}
	@RequestMapping(value = "/findPermissionByPermName", method = RequestMethod.POST)
	public Map<String,Object> findPermissionByPermName(@RequestParam(value = "permName", required = true) String permName) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        PermissionBean permission = service.findPermissionByPermName(permName);
        result.put("success",true);
        result.put("data",permission);
        return result;
	}
	@RequestMapping(value = "/listActivePermissions", method = RequestMethod.POST)
	public Map<String,Object> listActivePermissions() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<PermissionBean> list = service.listActivePermissions();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/listAllPermissions", method = RequestMethod.POST)
	public Map<String,Object> listAllPermissions() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<PermissionBean> list = service.listAllPermissions();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/searchPermissionsByFieldsByPage", method = RequestMethod.POST)
	public Map<String,Object> searchPermissionsByFieldsByPage(@RequestParam(value = "page", required = false) Integer pagenum,@RequestParam(value = "rows", required = false) Integer pagesize,@RequestParam(value = "last", required = false) String lastFlag,@RequestParam(value = "permName", required = false) String permName,@RequestParam(value = "active", required = false) String active) throws Exception{
        PermissionBean permission = new PermissionBean();
        if (!StringUtil.isBlank(permName)) permission.setPermName(permName);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) permission.setActive(activeBool);
        if (pagesize==null || pagesize <= 0) pagesize = 10;
        Integer recordCount = service.countSearchPermissionsByFieldsRecords(permission);
        Integer pageCount = (int)Math.ceil((double)recordCount/pagesize);
        if (pageCount <= 1) pageCount = 1;
        if (pagenum==null || pagenum <= 1) pagenum = 1;
        if (pagenum >= pageCount) pagenum = pageCount;
        Boolean lastFlagBool = BooleanUtil.parseBoolean(lastFlag);
        if (lastFlagBool == null) lastFlagBool = false;
        if (lastFlagBool) pagenum = pageCount;
        Map<String,Object> result = new TreeMap<String,Object>();
        List<PermissionBean> permissionList = service.searchPermissionsByFieldsByPage(permission,pagesize,pagenum);
        result.put("success",true);
        result.put("rows",permissionList);
        result.put("total",recordCount);
        result.put("page",pagenum);
        return result;
	}
	@RequestMapping(value = "/softDeleteAllPermissions", method = RequestMethod.POST)
	public Map<String,Object> softDeleteAllPermissions(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteAllPermissions(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/softDeletePermission", method = RequestMethod.POST)
	public Map<String,Object> softDeletePermission(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeletePermission(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleOnePermission", method = RequestMethod.POST)
	public Map<String,Object> toggleOnePermission(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleOnePermission(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/togglePermission", method = RequestMethod.POST)
	public Map<String,Object> togglePermission(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.togglePermission(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/updatePermission", method = RequestMethod.POST)
	public Map<String,Object> updatePermission(@RequestBody PermissionBean permission) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.updatePermission(permission);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
}

